# -*- coding: utf-8 -*-
# @Time    : 2019/10/31 11:30
# @Author  : 岳保保
# @File    : AuthorityViews.py
# @Software: PyCharm
from django.db.models import Q

from crm.models import Authority, Role
from crm.response.AuthorityResponse import AuthoritySerializer, RoleSerializer
from crm.request.AuthorityRequest import AuthorityForm, RoleForm
from utils.utils.rest import RestAPIView


class AuthorityView(RestAPIView):
    """权限"""
    model_class = Authority
    request_verify_class = AuthorityForm
    response_serializer_class = AuthoritySerializer
    url_path = "authority/"
    api_tags = "权限管理Api"

    def condition_search_filter(self, request):
        """条件查询"""
        condition_filter = Q()
        condition_filter.connector = 'AND'
        condition_filter.children.append(('title__contains', request.data.get("title") or ""))
        if request.data.get("type"):
            condition_filter.children.append(('type', request.data.get("type")))
        if request.data.get("pid"):
            condition_filter.children.append(('pid', request.data.get("pid")))
        return condition_filter


class RoleView(RestAPIView):
    """角色"""
    model_class = Role
    request_verify_class = RoleForm
    response_serializer_class = RoleSerializer
    url_path = "role/"
    api_tags = "角色管理Api"
